<?php

class Client_Table_Regions extends Apk_Db_Table
{
	protected $_name      = 'regions';
    protected $_primary   = 'id';
    protected $_ERROR = '';
    protected $_lang;

    protected $max_size = 22;
    protected $min_size = 8;

    public function __construct($lang = 'ru')
    {
    	$this->_lang = $lang;
    	parent::__construct();
    }

    public function GetError()
    {
    	return $this->_ERROR;
    }

 	public function GetRegionsList($a = null, $limit = null)
    {
    	$ret = array();

		$sel = $this->getAdapter()->select()
					//->from($this->_name, array('id', 'name' => 'name_'.$this->_lang))
					->from($this->_name, array('id', 'name', 'koatuu'))
					->order(array('name'));

		$data = $this->setDecorator('simpleDecorator')->getList($sel)->toArray();


    	if(count($data) > 0 && !is_null($a))
    	foreach($data as $k => $v)
    	  	if($v['id'] == $a) $data[$k]['__attributes']['active'] = 1;



    	return $data;
    }

    public function GetCitiesList($a = null, $region_id = null, $district_id = null)
    {
    	$ret = array();

		$sel = $this->getAdapter()->select()
					->from('cities', array('id', 'name', 'koatuu'))
					->order('name');

		if($district_id > 0) $sel->where('id_district = ?', $district_id);

		$data = $this->setDecorator('simpleDecorator')->getList($sel)->toArray();

    	if(count($data) > 0 && !is_null($a))
    	foreach($data as $k => $v)
    	  	if($v['id'] == $a) $data[$k]['__attributes']['active'] = 1;

    	return $data;
    }


    public function GetDistrictList($a = null, $region_id = null)
    {
    	$ret = array();

		$sel = $this->getAdapter()->select()
					->from('districts', array('id', 'name', 'koatuu'))
					->order('name');

		if(!is_null($region_id)) $sel->where('id_region = ?', $region_id);

		$data = $this->setDecorator('simpleDecorator')->getList($sel)->toArray();

    	if(count($data) > 0 && !is_null($a))
    	foreach($data as $k => $v)
    	  	if($v['id'] == $a) $data[$k]['__attributes']['active'] = 1;

    	return $data;
    }


   public function GetCitiesFilter($limit = 20)
    {
		$sel = $this->getAdapter()->select()
					->from('accounts', array())
					->joinInner('cities', 'cities.id = accounts.id_city', array('id', 'name' => 'name_ru', 'count_accounts'))
					->where('accounts.is_active')
					->order('cities.count_accounts desc')
					->group('cities.id')
					->limit($limit);

		$a = $this->getList($sel)->toArray();

		if(!(count($a) > 0)) return;

		$max = $a[0]['count_accounts'];
		$min = $a[count($a)-1]['count_accounts'];

		$a = $this->array_rand($a);
		//usort($a, "cmp");

		$kf = $this->max_size/$max;

		foreach ($a as $k=>$v)
		{
			$size = $v['count_accounts']*$kf;
			$a[$k]['size'] = $size;
		}

		return $a;
    }

    /********************
     ***  Decorators  ***
    *********************/

    public function simpleDecorator($key, $value)
    {
        switch ($key) {
            case 'public_date':
            case 'modify_date':
            case 'add_date':
            	$result[$key]['__attributes'] = $this->timestampToArray($value);
            	break;
            case 'name':
           	    $result[$key] = mb_substr($value, 0, 1).mb_strtolower(mb_substr($value, 1));
				break;
            default:
                $result[$key] = stripslashes($value);
                break;
        }
        return $result;
    }

  public function array_rand($a)
    {
		if(!is_array($a)) return;

    	srand($this->make_seed());
		$r = array();
		while(count($r) != count($a))
		{
			$s = rand(0, count($a)-1);
			if(!in_array($s, $r)) $r[] = $s;
		}

		$ret = array();
		foreach($r as $v)
			$ret[] = $a[$v];

		return $ret;
    }

	function make_seed() {
	    list($usec, $sec) = explode(' ', microtime());
	    return (float) $sec + ((float) $usec * 100000);
	}

	function cmp($a, $b){
		return strcmp($a["name"], $b["name"]);
	}



}
?>
